﻿using System.Diagnostics;

namespace DimensionsHelper.LanguageService.Syntax;

public class ConstantSignature : BindableSignature
{
    private Expression? _initializer;

    internal ConstantSignature(SyntaxToken constKeywordToken, Identifier name, SyntaxNode? parent) :
        base(SyntaxKinds.ConstantSignature, constKeywordToken, name, parent)
    {
    }


    /// <summary>
    ///     初始化表达式，在签名中，可以忽略此表达式。
    /// </summary>
    public Expression? Initializer => _initializer;


    /// <summary>
    ///     如果常量签名在类中，此属性为直接包含此常量的类签名。
    /// </summary>
    public ClassSignature? ContainingClass { get; internal set; }


    /// <summary>
    ///     直接包含此常量签名的模块，如果<see cref="ContainingClass" />不是<see langword="null" />，
    ///     此值应当和<see cref="ContainingClass" />的<see cref="ObjectTypeSignature.ContainingModule" />
    /// </summary>
    public ModuleSignature? ContainingModule { get; internal set; }


    public ConstantSignature Update(Expression initializer)
    {
        SetChildNullableField(ref _initializer, Expression.IsEmptyExpression, initializer);
        return this;
    }


    public override void AcceptVisitor<T>(SyntaxNodeVisitor<T> visitor)
    {
        Accept(visitor, Initializer);
        base.AcceptVisitor(visitor);
    }
}


public static partial class NodeFactory
{
    public static ConstantSignature CreateConstantSignature(SyntaxNode? parent, SyntaxToken constKeywordToken,
        Identifier name, Expression? initializer)
    {
        Debug.Assert(constKeywordToken.IsNode(SyntaxKinds.ConstKeywordToken));
        ConstantSignature signature = new(constKeywordToken, name, parent);
        if (initializer != null)
        {
            signature.Update(initializer);
        }

        return signature;
    }
}